package unit8;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Ex8_4_Example8_4 {

    // 1次读取多个字节
    static public void readFile() throws IOException {
        InputStream input = null;
        try {
            // 创建一个FileInputStream对象
            input = new FileInputStream("C:\\stu\\student1.txt");
            // 定义32个字节大小的缓冲区:
            byte[] buffer = new byte[32];

            int count = 0; //读取到的字节数
            int n = 0; //每次读取的字节数
            // 调用read()方法，每次读取多个字节，直到返回-1
            while ((n = input.read(buffer)) != -1) {
                String str = new String(buffer);
                System.out.print(str);
                count = count + n;
            }
            System.out.println("\nread " + count + " bytes.");
        }
        finally {
            if (input != null) {
                // 关闭流释放底层资源
                input.close();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        readFile();
    }

}
